package com.pascal91.webpp.controller;

import com.pascal91.webpp.model.Users;
import com.pascal91.webpp.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
public class RegistrationController {

    @Autowired
    UsersService usersService;

    @RequestMapping("/registration.html")
    public ModelAndView home(Model model) {
        Users user = new Users();
        model.addAttribute("user", user);
        return new ModelAndView("registration");
    }

    @RequestMapping(value = "/register/", method = RequestMethod.POST)
    public ModelAndView saveUser(Users user, HttpSession httpSession,
                                 @RequestParam(value = "re_password", required = true) String re_password){

        if(user.getPassword().equals(re_password)){
            usersService.addUser(user);
            httpSession.setAttribute("user", user);
        }

        return new ModelAndView("redirect:/home.html");
    }
}
